package com.manyiaby.view.viewutils;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.manyiaby.view.activityview.SearchActivity;

/**
 * Created by yx on 2017/7/14.
 */

public class SoftKeyBoardUtils {

    /**
     * 强制隐藏软键盘
     * 如果当前未显示则显示出来。如果显示出来，则隐藏。
     * 关闭Activity全局显示的输入框
     */
    public void hideSoftKeyBoar() {
        // 先隐藏键盘
        try {

            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            View currentFocus = getCurrentFocus();
            if (currentFocus != null) {
                //有焦点关闭 - 需在当前Activity界面某按钮调用
                LogUtil.iYx("关闭1");
                imm.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            } else {
                //无焦点关闭 - 可在onDestroy()调用
                LogUtil.iYx("关闭2");
                imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 隐藏软键盘
     * 关闭输入框触发焦点打开的输入框
     *
     * @param context
     * @param editTextView 获取焦点的控件
     */
    public static void hideSoftKeyBoar(Context context, View editTextView) {
        // 先隐藏键盘
        try {
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(editTextView.getWindowToken(), 0); //强制隐藏键盘
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //打开
    public static void oPenSoftKeyBoar(Context context) {
        try {
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
